<?php
    require_once '../config.php';
    
    function generateKey(){
        $letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        $output = "";
        for($i = 0; $i < 10; $i++){
            $output .= $letters[mt_rand(0, strlen($letters)-1)];
        }
        return $output;
    }
    
    if(isset($_GET['action']) && $_GET['action'] == "register"){
        // Get fields
        $app_name = mysqli_real_escape_string($db, trim($_POST['application_name']));
        $app_desc = mysqli_real_escape_string($db, trim($_POST['application_desc']));
        $dev_mail = strtolower(mysqli_real_escape_string($db, trim($_POST['dev_mail'])));
        $dev_name = mysqli_real_escape_string($db, trim($_POST['dev_name']));
        
        // Validate fields
        if($app_name == "" || $app_desc == "" || $dev_mail == "" || $dev_name == ""){
            $error[] = "Please fill out fields!";
        } else {
            $app_key = generateKey();
            $query = "INSERT INTO users (app_name, app_desc, app_key, dev_name, dev_mail) VALUES ('$app_name', '$app_desc', '$app_key', '$dev_name', '$dev_mail');";
            if(mysqli_query($db, $query)){
                $success[] = "Your application was successfully registered.<br />Application Name: <b>$app_name</b><br />Developer E-Mail: <b>$dev_mail</b><br />Your FHPI-Key is <b>$app_key</b>.<br /><br />Print this page and keep it in a safe place, you will need the FHPI key to use FHPI.";
                unset($_POST);
            } else {
                $error[] = "We could not sign you up for our service. Make sure you did not use this e-mail-address before.";
            }
        }
    }
    
    include '_header.php';
?>

        <h1>Register Your Application</h1>
        
        <?php
        
        if(isset($success)){
            foreach($success as $s){
                echo '<div class="msg success">'.$s.'</div>';
            }
        }
        
        if(isset($error)){
            foreach($error as $a){
                echo '<div class="msg error">'.$a.'</div>';
            }
        }
        
        ?>
        
        <div>If you want to use FHPI for your application you have to register it for free.</div>
        
        <h2>Why register?</h2>
        
        <div>Registering helps us preventing abuse. <br />
            You are welcome to create multiple accounts, one for each application. Make sure to use different mail-addresses though.<br />
            Signing up is free and also enables you to see FHPI usage statistics for your application.<br /><br />
            Please fill out all of the following fields.<br /><br />
        </div>
        
        <form name="register" method="post" action="?action=register">
            <table>
                <tr>
                    <th>Application Name</th>
                    <td><input type="text" name="application_name" value="<?php if(isset($_POST['application_name'])){echo $_POST['application_name'];}?>"/></td>
                </tr>
                <tr>
                    <th>Application Description</th>
                    <td><textarea name="application_desc"><?php if(isset($_POST['application_desc'])){echo $_POST['application_desc'];}?></textarea></td>
                </tr>
                <tr>
                    <th>Your Name</th>
                    <td><input type="text" name="dev_name" value="<?php if(isset($_POST['dev_name'])){echo $_POST['dev_name'];}?>"/></td>
                </tr>
                <tr>
                    <th>Your E-Mail</th>
                    <td><input type="text" name="dev_mail" value="<?php if(isset($_POST['dev_mail'])){echo $_POST['dev_mail'];}?>"/></td>
                </tr>
                <tr>
                    <th></th>
                    <td><input type="submit" name="submit" value="Register"/></td>
                </tr>
            </table>
        </form>
<?php include '_footer.php'; ?>